package com.baidu.model;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.baidu.basic.DateUtils;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

/**
 * 我喜欢的贴吧
 * @author libs
 * 2015年7月20日11:28:30
 */
public class MyTB extends Model<MyTB>{
	
	private static final long serialVersionUID = 282908528936842633L;
	public static final MyTB dao = new MyTB();
	
	/**
	 * 保存贴吧
	 * @param tb
	 */
	public void saveTB(MyTB tb) {
		//保存之前先判断是否已存在
		MyTB bean = MyTB.dao.findMyTbByFid(tb.getInt("fid"),tb.getStr("bdGuid"));
		if(bean == null){//不存在
			tb.set("guid", UUID.randomUUID().toString());
			tb.set("createTime", new Date());
			tb.set("isDeleted", false);
			tb.set("isHuLue",false);
			tb.save();
		}else{
			bean.set("ex", tb.getStr("ex"));
			bean.set("level",tb.getStr("level"));
			bean.set("levelName",tb.getStr("levelName"));
			bean.update();
		}
	}
	
	/**
	 * 根据fid和百度Guid获取我喜欢的贴吧
	 * @param fid
	 * @param bdGuid
	 */
	public MyTB findMyTbByFid(Integer fid, String bdGuid) {
		StringBuffer sql = new StringBuffer();
		sql.append("select a.* from my_tb a where 1=1");
		sql.append(" and a.isDeleted=false");
		sql.append(" and a.fid=?");
		sql.append(" and a.bdGuid=?");
		return MyTB.dao.findFirst(sql.toString(), fid, bdGuid);
	}
	
	/**
	 * 根据fid和百度Guid获取我喜欢的贴吧
	 * @param fid
	 * @param bdGuid
	 */
	public List<MyTB> queryTbs(String bdGuid) {
	    StringBuffer sql = new StringBuffer();
	    sql.append("select a.* from my_tb a where 1=1");
	    sql.append(" and a.isDeleted=false");
	    sql.append(" and a.bdGuid=?");
	    return MyTB.dao.find(sql.toString(), bdGuid);
	}
	
	/**
	 * 查询已签到贴吧的数量
	 * @param userGuid
	 * @param bdUserGuid
	 * @return
	 */
	public Integer querySignCount(String userGuid, String bdUserGuid) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) as signCount from my_tb a");
		sql.append(" left join bd_user b on a.bdGuid=b.guid");
		sql.append(" left join sys_user c on b.userGuid=c.guid");
		sql.append(" where a.isDeleted=false");
		sql.append(" and (a.signTime is not null");
		sql.append(" and  DATE_FORMAT(a.signTime,'%Y-%m-%d') =  DATE_FORMAT(?,'%Y-%m-%d'))");
		//sql.append(" or a.signTime is null)");
		sql.append(" and a.isHuLue=false");
		sql.append(" and b.userGuid=?");
		if(StrKit.notBlank(bdUserGuid)){
			sql.append(" and b.guid='"+bdUserGuid+"'");
		}
		return Db.queryLong(sql.toString(), DateUtils.realTime("Asia/Shanghai"), userGuid).intValue();
	}
	
	/**
	 * 根据用户guid查询可以签到的贴吧
	 * @param userGuid
	 * @return
	 */
	public List<MyTB> querySignTB(String userGuid) {
		StringBuffer sql = new StringBuffer();
		sql.append("select a.*,b.bduss as bduss from my_tb a");
		sql.append(" left join bd_user b on a.bdGuid=b.guid");
		sql.append(" left join sys_user c on b.userGuid=c.guid");
		sql.append(" where a.isDeleted=false");
		sql.append(" and ((a.signTime is not null");
		sql.append(" and DATE_FORMAT(a.signTime,'%Y-%m-%d') < DATE_FORMAT(?,'%Y-%m-%d'))");
		sql.append(" or a.signTime is null)");
		sql.append(" and a.isHuLue=false");
		if(StrKit.notBlank(userGuid)){
			sql.append(" and b.userGuid=?");
			return find(sql.toString(), DateUtils.realTime("Asia/Shanghai"), userGuid);
		}else{
			return find(sql.toString(), DateUtils.realTime("Asia/Shanghai"));
		}
	}
	
	/**
	 * 根据贴吧名和bdguid 查询是否关注该贴吧
	 * @param name
	 * @param bdGuid
	 * @return
	 */
	public MyTB findByNameAndBdId(String name, String bdGuid) {
		StringBuffer sql = new StringBuffer();
		sql.append("select a.* from my_tb a");
		sql.append(" where a.isDeleted=false");
		sql.append(" and a.bdGuid=?");
		sql.append(" and a.name=?");
		return findFirst(sql.toString(), bdGuid, name);
	}
	
	/**
	 * 根据百度账号 删除喜欢的贴吧
	 * @param guid
	 */
    public void delByBDUserId(String guid) {
        Db.update("delete from my_tb where bdGuid = ?", guid);
    }
}
